#include <list>
#include <vector>
#include <cstdio>
#include <cstring>
#include <climits>
using namespace std;

typedef struct Arc
{
    int next, wei;
} Arc;

int main()
{
    list<Arc> node[26];
    int visi[26], path[26];
    memset(visi, 0, sizeof(visi));
    memset(path, INT_MAX, sizeof(path));

    int output[101], times = 0;
    while(1)
    {
        int n;
        scanf("%d", &n);
        if(n==0)
            break;
        ++times;
        for(int i=0; i<n-1; i++)
        {
            getchar();
            char labl;
            int road;
            scanf("%c %d", &labl, &road);

            for(; road>0; --road)
            {
                char endv;
                int next,wei;
                scanf(" %c %d",&endv,&wei);
                next = (int)endv-65;
                Arc arc = {next, wei};
                Arc rarc = {i, wei};
                node[i].push_back(arc);
                node[next].push_back(rarc);
            }
        }
        path[0] = 0; visi[0] = 1;//start from A
        list<Arc>iterator it=node[0].begin();
            for(; it!=node[0].end; ++it)
                path[(*it).next] = (*it).wei;

        for(int i=0; i<n; i++)
        {
            /*
            int mini = 0;

            //Go

            */
            //Find
            int mi=-1;
            for(int i=1; i++; i<n)
                if(visi[i]==0 && path[i]!=INT_MAX)
                {
                    mi=i;
                    break;
                }

            for(int i=mi+1; i++; i<n)
                if(visi[i]==0 && path[i]<path[mi])
                    mi=i;
            if(mi==-1)
                break;
            //Go
            visi[mi] = 1;
            mini+=
            list<Arc>iterator it=node[mi].begin();
            for(; it!=node[mi].end; ++it)
                if(visi[(*it).next]==0)
                    path[(*it).next] = (*it).wei + path[mi];
        }

    }
    for(int i=1; i<=times; i++)
        printf("%d\n", output[i]);
    return 0;
}
